package com.yiwei.zxp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.allen.library.SuperButton;
import com.yiwei.zxp.R;
import com.yiwei.zxp.api.model.CateGoryModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class GoodsTypeAdapter extends ItemAdapter<CateGoryModel.DatasBean, GoodsTypeAdapter.DefaultHolder> {


    public GoodsTypeAdapter(Context context) {
        super(context);
    }

    private int selectPoi = -1;

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_goods_type, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        CateGoryModel.DatasBean item = getItem(position);
        holder.item = item;
        holder.poi = position;
        if (selectPoi == position) {
            holder.btnType.setTextColor(0xffffffff);
            holder.btnType.setShapeSolidColor(0xffFF0030).setUseShape();
        } else {
            holder.btnType.setTextColor(0xff000000);
            holder.btnType.setShapeSolidColor(0xffEEEEEE).setUseShape();
        }
        holder.btnType.setText(item.catname);
    }

    class DefaultHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.btn_type)
        SuperButton btnType;
        private int poi;
        private CateGoryModel.DatasBean item;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            btnType.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            selectPoi = poi;
            notifyDataSetChanged();
            if (onChooseListener != null) {
                onChooseListener.onClick(item.id, item.catname);
            }
        }
    }


    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String id, String name);
    }
}
